package com.controller.sale;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import com.common.model.Global;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.common.DateUtils;
import com.common.http.HttpResult;
import com.service.sale.ContractSignService;
import com.model.sale.ContractSignModel;

/**
 * 合同签署
 */
@Controller("contractSignController")
@RequestMapping(value = "/api")
public class ContractSignController {
    static final Logger logger = LoggerFactory.getLogger(ContractSignController.class);

    @Autowired
    ContractSignService contractSignService;

    /**
     * 查询列表
     *
     * @param map
     * @return
     */
    @RequestMapping(value = "sale/contract/sign/selectList", method = {RequestMethod.POST})
    @ResponseBody
    public HttpResult selectList(@RequestParam Map<String, Object> map) {
        return contractSignService.selectList(map);
    }

    /**
     * 查询详情
     *
     * @param map
     * @return
     */
    @RequestMapping(value = "sale/contract/sign/selectInfor", method = {RequestMethod.GET})
    @ResponseBody
    public HttpResult selectInfor(@RequestParam String id) {
        return contractSignService.selectInfor(id);
    }

    /**
     * 保存询价
     *
     * @param
     * @return
     */
    @RequestMapping(value = "sale/contract/sign/save", method = {RequestMethod.POST})
    @ResponseBody
    public HttpResult save(@RequestBody ContractSignModel contractSignModel) {
        if (null == contractSignModel) {
            return HttpResult.error("合同签署信息不得为空");
        }
        String longTerm = contractSignModel.getLongTerm();
        if ((Global.YES + "").equals(longTerm)) {
            return contractSignService.saveLong(contractSignModel);
        }else {
            return contractSignService.save(contractSignModel);
        }

    }

    /**
     * 删除询价
     *
     * @param
     * @return
     */
    @RequestMapping(value = "sale/contract/sign/delete", method = {RequestMethod.DELETE})
    @ResponseBody
    public HttpResult delete(@RequestParam String id) {
        return contractSignService.delete(id);
    }

    /**
     * 导出
     *
     * @param map
     * @param response
     * @throws IOException
     */
    @RequestMapping(value = "sale/contract/sign/export")
    public void export(@RequestParam Map<String, Object> map, HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("采购订单-" + DateUtils.getCurrentDate(), "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        contractSignService.export(map, response);

    }
}
